﻿using AuthTemplate.DTO.Relevance;
using AuthTemplate.DTO.User;
using AuthTemplate.Entity;
using AuthTemplate.Entity.Common;
using AuthTemplate.Infrastructure.AutoMapper;
using AuthTemplate.Repository.Interface;
using AuthTemplate.Repository.Interface.IEntityRepository;
using AuthTemplate.Repository.Repositories;
using AuthTemplate.ViewModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthTemplate.Service
{
    public class RelevanceService : BaseService, IRelevanceService
    {
        private readonly IRelevanceRepository relevanceRepository;
        private readonly IUnitOfWork unitOfWork;
        public RelevanceService(IRelevanceRepository relevanceRepository,
            IUnitOfWork unitOfWork)
        {
            this.relevanceRepository = relevanceRepository;
            this.unitOfWork = unitOfWork;
        }

        public void AddRelevance(AddRelevanceInput input)
        {
            Relevance relevance = input.Map<Relevance>();
            relevance.Status = 1;

            relevanceRepository.Add(relevance);
            unitOfWork.Commit();
        }

        public void EditRelevance(EditRelevanceInput input)
        {
            Relevance relevance = input.Map<Relevance>();
            List<string> paras = new List<string>()
            {
                "Description","Status","OperateTime","OperatorId",
                "FirstId","SecondId","FirstName","SecondName"
            };

            relevanceRepository.Edit(relevance, paras.ToArray());
            unitOfWork.Commit();
        }

        public void DeleteRelevance(string id)
        {
            Relevance relevance = new Relevance();
            relevance.Id = id;
            relevanceRepository.Delete(relevance);
            unitOfWork.Commit();
        }

        public Dictionary<string, string> GetAllRelevanceType()
        {
            return RelevanceType.RelevanceTypeName;
        }

        public List<Relevance> GetAllRelevance(int page, int limit, string key, string firstId, out int count)
        {
            var result = relevanceRepository.GetAll();
            if (!string.IsNullOrEmpty(key))
            {
                result = result.Where(s => s.KeyType == key);
                if (!string.IsNullOrEmpty(firstId))
                {
                    result = result.Where(s => s.FirstId == firstId );
                }
            }
            count = result.Count();
            result = result.OrderBy(s => s.FirstName).Skip((page - 1) * limit).Take(limit);
            return result.ToList();

        }

    }

    public interface IRelevanceService
    {
        void AddRelevance(AddRelevanceInput input);
        void DeleteRelevance(string id);
        void EditRelevance(EditRelevanceInput input);
        List<Relevance> GetAllRelevance(int page, int limit, string key, string firstId, out int count);
        Dictionary<string, string> GetAllRelevanceType();
    }
}
